package com.sunny.ynhz.ui.ynts;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.github.mikephil.charting.charts.LineChart;
import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.base.FragmentViewNoTitlePagerAdapter;
import com.sunny.ynhz.bean.MoreLineProBlemBean;
import com.sunny.ynhz.bean.WxyyBean;
import com.sunny.ynhz.model.IProblemTypeResultView;
import com.sunny.ynhz.model.WxyyModel;
import com.sunny.ynhz.ui.base.AppBaseTranslationTitleBarActivity;
import com.sunny.ynhz.ui.ynts.problemover.ProblemStaticActivity;
import com.sunny.ynhz.utils.ChartUtils;
import com.sunny.ynhz.utils.DetailsMarkerView;
import com.sunny.ynhz.utils.SPUtil;
import com.sunny.ynhz.utils.TimeUtils;
import com.sunny.ynhz.utils.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by huhuan123 on 2018/8/11.
 *
 * @describe TODO
 */

public class WxyyActivity extends AppBaseTranslationTitleBarActivity implements IProblemTypeResultView, RadioGroup.OnCheckedChangeListener {
    List<Fragment> fragments = new ArrayList<>();
    @BindView(R.id.fragments)
    ViewPager viewPager;
    @BindView(R.id.tv_all_count)
    RadioButton tv_all_count;
    @BindView(R.id.tv_bj)
    RadioButton tv_bj;
    @BindView(R.id.tv_wait_check)
    RadioButton tv_wait_check;
    @BindView(R.id.tv_handler)
    RadioButton tv_handler;
    @BindView(R.id.mLineChart)
    LineChart lineChart;
    @BindView(R.id.tv_start_time)
    TextView tv_start_time;
    @BindView(R.id.tv_end_time)
    TextView tv_end_time;
    @BindView(R.id.tv_riverName)
    TextView tv_riverName;
    @BindView(R.id.rg_cage_culture)
    RadioGroup rg_cage_culture;
    List<Integer> mConuntList = new ArrayList<>();
    List<Float> havDoneCountList = new ArrayList<>();
    List<Float> havDonePreList = new ArrayList<>();

    //  if (mriver_type <= 6) {
//        rnText = "州";
//    } else if (mriver_type <= 11) {
//        rnText = "县市";
//    } else if (mriver_type <= 15) {
//        rnText = "镇";
//    } else if (mriver_type == 16) {
//        rnText = "村";
//    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wxyy);
        String type = getIntent().getStringExtra("type");
        String title = getIntent().getStringExtra("title");
        setTitle(title);

        ChartUtils.initChart(lineChart, this);
        rg_cage_culture.setOnCheckedChangeListener(this);
        int userType = Integer.parseInt(SPUtil.get("userType", "").toString());
        for (int i = 0; i < 4; i++) {
            WxyyFragment instanceWxyyFragment = WxyyFragment.getInstanceWxyyFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("position", i);
            bundle.putString("type", type);
            instanceWxyyFragment.setArguments(bundle);
            fragments.add(instanceWxyyFragment);
        }
        FragmentViewNoTitlePagerAdapter fragmentViewNoTitlePagerAdapter = new FragmentViewNoTitlePagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(fragmentViewNoTitlePagerAdapter);
        viewPager.setOffscreenPageLimit(4);
        WxyyModel wxyyModel = new WxyyModel(this, this);
        wxyyModel.getWxYYBean(type);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    tv_all_count.setChecked(true);
                } else if (position == 1) {
                    tv_bj.setChecked(true);
                } else if (position == 2) {
                    tv_handler.setChecked(true);

                } else if (position == 3) {
                    tv_wait_check.setChecked(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public void setTextView(List<WxyyBean.DateBean> mList) {
        WxyyBean.DateBean dateBean = mList.get(0);
        int pcount = dateBean.getPCOUNT();
        int dcount = dateBean.getDCOUNT();
        int ycount = dateBean.getYCOUNT();
        tv_all_count.setText("总数\n" + pcount);
        tv_bj.setText("已办结\n" + ycount);
        tv_wait_check.setText("待复核\n" + dcount);
        tv_handler.setText("处理中\n" + dateBean.getWCOUNT());
    }

    public void displayTimeSelect(int type, String currentTime) {
        if (currentTime.contains("请选择时间")) {
            currentTime = TimeUtils.getNowData();
        }
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        String sysYear = TimeUtils.getSysYear();
        int mCurrentMonth = TimeUtils.getCurrentMonth();
        String nowDate = TimeUtils.getNowDay();
        startDate.set(Integer.parseInt(sysYear) - 2, 0, 1);
        endDate.set(Integer.parseInt(sysYear), mCurrentMonth - 1, Integer.parseInt(nowDate));
        TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = formatter.format(date);

                if (type == 1) {
                    tv_end_time.setText(dateString);
                } else {
                    tv_start_time.setText(dateString);
                }
            }
        }).setDate(TimeUtils.strToCalendar(currentTime)).setRangDate(startDate, endDate).build();
        pvTime.show();

    }

    @Override
    public void onSuccess(Object wxyyBean) {
        if (wxyyBean instanceof MoreLineProBlemBean) {
            MoreLineProBlemBean moreLineProBlemBean = (MoreLineProBlemBean) wxyyBean;
            List<MoreLineProBlemBean.DateBean> date = moreLineProBlemBean.getDate();
            String[] month = new String[date.size()];
            for (int i = 0; i < date.size(); i++) {
                MoreLineProBlemBean.DateBean dateBean = date.get(i);
                int ycount = dateBean.getYCOUNT();
                havDoneCountList.add((float) ycount);
                int pcount = dateBean.getPCOUNT();
                mConuntList.add(pcount);
                if (pcount == 0) {
                    havDonePreList.add((float) 0);
                } else {
                    float haveDonePre = ycount / (float) pcount;
                    havDonePreList.add(haveDonePre * 100);
                }
                String[] split = dateBean.getMOUTH().split("-");
                month[i] = split[1];
            }

            ChartUtils.showLineChart(mConuntList, "总数", Color.parseColor("#E2B25F"), lineChart, month);
            ChartUtils.addLine(havDoneCountList, "已办结", Color.parseColor("#6EE4A2"), lineChart);
            ChartUtils.addLine(havDonePreList, "办结率", Color.parseColor("#79C6EC"), lineChart);
            DetailsMarkerView detailsMarkerView = new DetailsMarkerView(WxyyActivity.this, R.layout.item_mark_view);
            detailsMarkerView.setChartView(lineChart);
            lineChart.setMarker(detailsMarkerView);
//一定要设置这个玩意，不然到点击到最边缘的时候不会自动调整布局
        }
    }

    @Override
    public void onCache() {

    }

    @Override
    public void onFail() {

    }

    @Override
    public void onStartLoad() {

    }

    @OnClick({R.id.tv_end_time, R.id.tv_start_time, R.id.tv_look_for})
    public void onClickView(View view) {
        if (view.getId() == R.id.tv_end_time) {
            displayTimeSelect(1, tv_end_time.getText().toString());
        } else if (view.getId() == R.id.tv_start_time) {
            displayTimeSelect(0, tv_start_time.getText().toString());
        } else if (view.getId() == R.id.tv_look_for) {
            if (!(tv_start_time.getText().toString().contains("请选择时间") && tv_end_time.getText().toString().contains("请选择时间"))) {
                if (tv_start_time.getText().toString().contains("请选择时间")) {
                    ToastUtils.SingleToastUtil(WxyyActivity.this, "请选择开始时间");
                    return;
                }
                if (tv_end_time.getText().toString().contains("请选择时间")) {
                    ToastUtils.SingleToastUtil(WxyyActivity.this, "请选择结束时间");
                    return;
                }
            }
            if ((tv_start_time.getText().toString().contains("请选择时间") && tv_end_time.getText().toString().contains("请选择时间"))) {
                for (int i = 0; i < fragments.size(); i++) {
                    WxyyFragment wxyyFragment = (WxyyFragment) fragments.get(i);
                    wxyyFragment.notyData("", "", tv_riverName.getText().toString());
                }
                return;
            }


            long startTime = TimeUtils.strTimeToStamp(tv_start_time.getText().toString());
            long endTime = TimeUtils.strTimeToStamp(tv_end_time.getText().toString());
            if (endTime < startTime) {
                ToastUtils.SingleToastUtil(WxyyActivity.this, "开始时间不能大于结束时间");
                return;
            }
            for (int i = 0; i < fragments.size(); i++) {
                WxyyFragment wxyyFragment = (WxyyFragment) fragments.get(i);
                wxyyFragment.notyData(tv_start_time.getText().toString(), tv_end_time.getText().toString(), tv_riverName.getText().toString());
            }

        }
    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        if (R.id.tv_all_count == i) {
            viewPager.setCurrentItem(0);

        } else if (R.id.tv_bj == i) {
            viewPager.setCurrentItem(1);

        } else if (R.id.tv_wait_check == i) {
            viewPager.setCurrentItem(3);
        } else if (R.id.tv_handler == i) {
            viewPager.setCurrentItem(2);

        }

    }
}
